<template>
  <div class="page-container">
    <page_breadcrumb :getKeyMethod="init_table_model" @change="change_table_model" pageOption />
    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e"
      >
      <template v-slot:action-right>
        <el-upload ref="upload" :show-file-list="false" action="/oms-api/refund/ReturnConfirmOrderRpc/importExcel.json"
          v-auth="'oms_return_good_confirm_import'" style="display: inline;" :on-success="uploadExcelSuccess"
          :on-error="uploadExcelError">
          <el-button slot="trigger" size="mini" type="primary">{{ $tt('label_import_excel') }}</el-button>
        </el-upload>
      </template>
    </ex_super_table_page>
  </div>
</template>
<script>
import business_function from "@/common/business_function"

import superTablePageFunction from '@/mixin/super-table-page-function';
import emitOnFunction from '@/mixin/emit-on-function';

import page_table_model_manage from '@/mixin/page_table_model_manage';
let emitKey = 'return_good>confirm'
export default {
  title: 'title_return_address_confirm',
  mixins: [superTablePageFunction, emitOnFunction(emitKey), page_table_model_manage],
  components: {},


  data() {
    return {
      selection: [],

      option: {
        search: {
          model: [
            { label: "label_order_code", key: "code", },
            { label: "label_track_code", key: "trackingNumber", },
            { label: "label_status", key: "status", type: 'select', enum: 'OmsReturnConfirmOrderStatusEnum', },
            { label: "label_default_consignee_person_phone", key: "consigneePhone", },
            { label: "label_default_consignee_person_name", key: "defaultConsignee", },
            { label: "label_confirm_consignee_person_name", key: "confirmConsignee", },
            { label: "label_import_time", key: "importTime", type: 'date-picker', attrs: { type: 'datetimerange' }, keyExtra: { _emitKey: 'input', 0: 'gmtCreateStart', 1: 'gmtCreateEnd' } },
            { label: "label_confirm_time", key: "confirmTime", type: 'date-picker', attrs: { type: 'datetimerange' }, keyExtra: { _emitKey: 'input', 0: 'gmtModifyStart', 1: 'gmtModifyEnd' } },
          ],
        },
        actionExtra: [
          { label: 'label_export', attrs: { type: "primary" }, auth: "oms_return_good_confirm_export", click: this.exportExcel },
          {
            label: 'label_batch_send_sms', attrs: { type: "" },
            auth: 'oms_return_good_confirm_send_sms',
            click: e => {
              if (_.isEmpty(this.selection)) return this.$message.error('tip_please_select_order');
              this.sendSms(this.selection.map(it => it.id))
            }
          },
          { label: 'label_download_excel_import_temp', click: this.downloadImportTemp },
        ],
        table: {
          url: "/oms-api/refund/ReturnConfirmOrderRpc/selectList.json",
          urlDataHandle: this.dataHandle,

          model: this.init_table_model(),
        },
      },
    };
  },



  methods: {
    option_table_model() {
      return [
        { type: 'selection', clickToggle: true, keyGroup: 'label_table_operate' },
        {
          type: 'expandDesc', 'width': 100,
          expandDesc: {
            column: 4, border: true, direction: 'vertical',
            labelClassName: 'widthdiv4', contentClassName: 'widthdiv4',
            model: [],
          },
        },
        { key: 'code', label: 'label_order_code', 'show-overflow-tooltip': true, },
        { key: 'trackingNumber', label: 'label_track_code', 'show-overflow-tooltip': true, },
        { key: 'isVisits', label: 'label_is_has_visit_page', 'width': 120, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum") },
        { key: 'logisticsProvider', label: 'label_logistics_supplier', 'width': 120, 'show-overflow-tooltip': true, },

        {
          key: 'historyAddress', label: 'label_default_consignee_address', width: 120,
          render: (h, { row, column }) => {
            const code = row[column.property]
            if (!code) return this.$tt('data_temp_none')
            return (<text_extra_tooltip_operate
              value={code} ellipsis
              data_type={column.property} data_id={row.id}
            />)
          },

        },
        {
          key: 'historyConsignee', label: 'label_default_consignee_person_name', width: 120, 'show-overflow-tooltip': true,
          // formatter: (r, c, v, i) => r.historyOrderAddressDTO ? r.historyOrderAddressDTO.consignee : r.defaultOrderAddressDTO.consignee
        },
        {
          key: 'historyConsigneePhone', label: 'label_default_consignee_person_phone', width: 120, 'show-overflow-tooltip': true,
          // formatter: (r, c, v, i) => r.historyOrderAddressDTO ? r.historyOrderAddressDTO.consigneePhone : r.defaultOrderAddressDTO.consigneePhone
        },
        {
          key: 'defaultAddress', label: 'label_confirm_consignee_address', width: 120,
          render: (h, { row, column }) => {
            const code = row[column.property]
            if (!code) return this.$tt('data_temp_none')
            return (<text_extra_tooltip_operate
              value={code} ellipsis
              data_type={column.property} data_id={row.id}
            />)
          },
        },
        {
          key: 'defaultConsignee', label: 'label_confirm_consignee_person_name', width: 120, 'show-overflow-tooltip': true,
          // formatter: (r, c, v, i) => r.status == 'PROCESSED' ? r.defaultOrderAddressDTO.consignee : '暂无'
        },
        {
          key: 'defaultConsigneePhone', label: 'label_confirm_consignee_person_phone', width: 120, 'show-overflow-tooltip': true,
          // formatter: (r, c, v, i) => r.status == 'PROCESSED' ? r.defaultOrderAddressDTO.consigneePhone : '暂无'
        },

        { key: "gmtCreate", label: "label_import_time", width: 120, formatter: (r, c, v, i) => this.$dateFormat(v) },
        { key: "gmtModify", label: "label_confirm_time", width: 120, formatter: (r, c, v, i) => r.status == 'PROCESSED' ? this.$dateFormat(v) : '' },
        {
          key: 'status', label: 'label_status', 'min-width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$enumShow(v, "OmsReturnConfirmOrderStatusEnum")
        },
        {
          key: 'orderWeight', label: 'label_origin_order_weight', 'width': 120, 'show-overflow-tooltip': true,
          // formatter: (r, c, v, i) => this.$enumShow(v, "BizTypeEnum")
        },


        { key: 'warehouseName', label: 'label_returnGood_warehouse', 'width': 100, formatter: (r, c, v, i) => this.$formatNameCode(v, r.warehouseCode) },
        { key: 'bizType', label: 'label_business_type', 'width': 100, formatter: (r, c, v, i) => this.$enumShow(v, "BizTypeEnum") },
        { key: 'returnReason', label: 'label_returnGood_reason', 'width': 100, },
        {
          type: 'operate', width: 160, operate: [
            {
              label: "label_send_sms",
              auth: 'oms_return_good_confirm_send_sms',
              disabled: (row) => ['PROCESSED'].includes(row.status),
              click: (row, i) => {
                this.sendSms([row.id])
              }
            },
            {
              label: 'label_delete',
              auth: 'oms_return_good_confirm_delete',
              type: "confirm-button", buttonClass: "button_text_danger", confirmButtonType: 'danger',
              url: '/oms-api/refund/ReturnConfirmOrderRpc/remove.json', message: 'text_confirm_sure_delete',
              params: (v) => { return { id: v.id } }, done: ['reload', 'tip']
            },
          ]
        }
      ]
    },
    dataHandle(res) {
      res.data.rows.forEach(row => {
        row.historyAddress = row.historyOrderAddressDTO ?
          `${row.historyOrderAddressDTO.country} ${row.historyOrderAddressDTO.province} ${row.historyOrderAddressDTO.city} ${row.historyOrderAddressDTO.county} ${row.historyOrderAddressDTO.address1}`
          :
          `${row.defaultOrderAddressDTO.country} ${row.defaultOrderAddressDTO.province} ${row.defaultOrderAddressDTO.city} ${row.defaultOrderAddressDTO.county} ${row.defaultOrderAddressDTO.address1}`

        row.historyConsignee = row.historyOrderAddressDTO ? row.historyOrderAddressDTO.consignee : row.defaultOrderAddressDTO.consignee
        row.historyConsigneePhone = row.historyOrderAddressDTO ? row.historyOrderAddressDTO.consigneePhone : row.defaultOrderAddressDTO.consigneePhone
        row.defaultAddress = row.status == 'PROCESSED' ?
          `${row.defaultOrderAddressDTO.country} ${row.defaultOrderAddressDTO.province} ${row.defaultOrderAddressDTO.city} ${row.defaultOrderAddressDTO.county} ${row.defaultOrderAddressDTO.address1}` :
          ''

        row.defaultConsignee = row.status == 'PROCESSED' ? row.defaultOrderAddressDTO.consignee : '暂无'
        row.defaultConsigneePhone = row.status == 'PROCESSED' ? row.defaultOrderAddressDTO.consigneePhone : '暂无'
      })

      return res.data.rows
    },
    sendSms(ids) {
      let tip = `已选择${ids.length}单据，确认发送短信吗?`


      this.$confirm(tip, this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/oms-api/refund/ReturnConfirmOrderRpc/sendSms.json",
          method: 'post', type: 'json',
          loading: "发送短信中",
          data: {
            ids
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          }
        })
      }).catch(() => {
      });

    },
    uploadExcelSuccess(response, file, fileList) {
      if (response.code == 0) {
        this.$message.success('上传成功');
        this.$emitPage("oms>import_manage", 'reload')
        this.$confirm(this.$tt("tip_importing_confirm_goto_recode_list"), this.$tt('label_tip'), {
          type: 'success'
        }).then(() => {
          this.$router.push({
            path: '/page/oms/report/import_manage',
          });
        }).catch(() => {

        });
      } else {
        this.$message.error(response.message || '上传错误');
      }
      if (this.$refs.upload) this.$refs.upload.clearFiles()
    },
    uploadExcelError(response, file, fileList) {
      this.$message.error('上传失败')
    },
    downloadImportTemp() {
      this.$downloadFile({
        loading: true,
        url: '/oms-api/refund/ReturnConfirmOrderRpc/downloadImportTemp.json',
      })
    },
    exportExcel() {
      const searchParams = this.$refs.SuperTablePage.getSearchFormData()
      delete searchParams.importTime
      delete searchParams.confirmTime
      business_function.confirmOperateBaseSelectOrForm.call(this, {
        data: searchParams,
        model: this.option.search.model,
        mustData: true,
        callback: (selection, data) => {
          let params = data
          this.$functionCommon({
            url: "/oms-api/refund/ReturnConfirmOrderRpc/exportExcel.json",
            data: params, type: 'form',
          })
        }
      })

    },
  }
};

</script>
<style></style>
